package com.ssms.controller;

import cn.hutool.core.io.FileUtil;
import com.ssms.commons.Constant;
import com.ssms.commons.Result;
import com.ssms.commons.StuQueryParam;
import com.ssms.commons.TablePage;
import com.ssms.model.Clazz;
import com.ssms.model.StuDetail;
import com.ssms.model.SysUser;
import com.ssms.service.ClazzService;
import com.ssms.service.SysUserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/sysUser")
public class SysUserController {

    @Value("${application.profile}")
    private String filePath;

    @Resource
    private SysUserService sysUserService;

    @RequestMapping()
    public String sysUser() {
        return "sysUser";
    }

    @RequestMapping("student")
    public String student() {
        return "student";
    }

    @RequestMapping("graduation")
    public String graduation() {
        return "graduation";
    }

    @RequestMapping("info")
    public String info() {
        return "stuInfo";
    }

    @RequestMapping("myScore")
    public String myScore() {
        return "myScore";
    }

    @RequestMapping("stuList")
    @ResponseBody
    public TablePage stuList(TablePage tablePage, StuQueryParam queryParam) {
        return sysUserService.searchStu(tablePage, queryParam);
    }

    @RequestMapping("graduations")
    @ResponseBody
    public TablePage graduations(TablePage tablePage, @RequestParam(value = "grade",required = false) Integer grade) {
        return sysUserService.searchGraduation(tablePage, grade);
    }

    @RequestMapping("list")
    @ResponseBody
    public TablePage list(TablePage tablePage,
                          @RequestParam(value = "username",required = false) String username,
                          @RequestParam(value = "sex",required = false) String sex) {
        return sysUserService.search(tablePage, username,sex);
    }

    @RequestMapping("all/{roleType}")
    @ResponseBody
    public Result all(@PathVariable("roleType") Integer roleType) {
        return sysUserService.all(roleType);
    }

    @RequestMapping("add")
    @ResponseBody
    public Result add(SysUser sysUser) {
        return sysUserService.save(sysUser);
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(SysUser sysUser) {
        return sysUserService.update(sysUser);
    }

    @RequestMapping("add/stu")
    @ResponseBody
    public Result addStu(SysUser sysUser, StuDetail stuDetail) {
        return sysUserService.addStu(sysUser,stuDetail);
    }

    @RequestMapping("update/stu")
    @ResponseBody
    public Result updateStu(SysUser sysUser, StuDetail stuDetail) {
        return sysUserService.updateStu(sysUser,stuDetail);
    }

    @RequestMapping("updateXjs")
    @ResponseBody
    public Result updateXjs(@RequestParam("ids[]")List<Integer> ids) {
        return sysUserService.updateXjs(ids);
    }

    @RequestMapping("uploadPic")
    @ResponseBody
    public Result uploadPic(@RequestParam("uid") Integer uId, @RequestParam("file") MultipartFile file) throws IOException {
        System.out.println("uid:" + uId);
        String originalFilename = file.getOriginalFilename();
        String val = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileName = System.currentTimeMillis() + val;
        File f = new File(filePath);
        if(!f.exists()){
            f.mkdir();
        }
        String path = filePath + fileName;
        FileUtil.writeBytes(file.getBytes(), path);

        SysUser stu = new SysUser();
        StuDetail stuDetail = new StuDetail();
        stuDetail.setUserId(uId);
        stuDetail.setPicPath("/file/" + fileName);
        return sysUserService.updateStu(stuDetail);
    }

    // 单个删除学生
    @RequestMapping("delete/stu")
    @ResponseBody
    public Result deleteStu(Integer id) {
        return sysUserService.deleteStu(id);
    }

    // 批量删除毕业生
    @RequestMapping("deleteStus")
    @ResponseBody
    public Result deleteStus(@RequestParam("ids[]")List<Integer> ids) {
        for (Integer id : ids) {
            sysUserService.deleteStu(id);
        }
        return Result.success();
    }

    //删除辅导员
    @RequestMapping("delete")
    @ResponseBody
    public Result delete(Integer id) {
        return sysUserService.delete(id);
    }

    @RequestMapping("import")
    @ResponseBody
    public Result imports(@RequestParam("file") MultipartFile file) throws IOException {
        return sysUserService.importStu(file);
    }

    @RequestMapping("getInfo")
    @ResponseBody
    public Result getInfo(Integer stuId) {
        return sysUserService.getInfo(stuId);
    }
}
